package com.formal.product;

import java.util.ArrayList;
import java.util.HashMap;

import android.app.ListActivity;
import android.app.ProgressDialog;
import android.graphics.Bitmap;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.SimpleAdapter;
import android.widget.SimpleAdapter.ViewBinder;

public class PremiereReminderActivity extends ListActivity {
	/** Called when the activity is first created. */
	private ProgressDialog pd = null;

	// private ArrayList<HashMap<String, Object>> temp = null;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

		this.pd = ProgressDialog.show(PremiereReminderActivity.this, "",
				"Loading. Please wait...", false);

		// Start a new thread that will download all the data
		new DownloadTask().execute("");
	}

	public class MyViewBinder implements ViewBinder {
		@Override
		public boolean setViewValue(View view, Object data,

		String textRepresentation) {

			if ((view instanceof ImageView) & (data instanceof Bitmap)) {

				ImageView iv = (ImageView) view;

				Bitmap bm = (Bitmap) data;

				iv.setImageBitmap(bm);
				return true;

			}
			return false;

		}
	}

	private class DownloadTask
			extends
			AsyncTask<String, ArrayList<HashMap<String, Object>>, ArrayList<HashMap<String, Object>>> {
		protected ArrayList<HashMap<String, Object>> doInBackground(
				String... args) {

			// This is where you would do all the work of downloading your data
			JsonParser jp = new JsonParser();
			ArrayList<HashMap<String, Object>> opening = jp.getOpening();
			ArrayList<HashMap<String, Object>> upcoming = jp.getUpComing();
			opening.addAll(upcoming);
			return opening;
		}

		protected void onPostExecute(ArrayList<HashMap<String, Object>> result) {
			// Pass the result data back to the main activity
			// result;
			SimpleAdapter saImageItems = new SimpleAdapter(
					PremiereReminderActivity.this, result, R.layout.tablelist,
					new String[] { "thumbnail", "title", "runtime",
							"release_date" },

					new int[] { R.id.poster, R.id.title, R.id.runtime,
							R.id.releasedate });

			saImageItems.setViewBinder(new MyViewBinder());
			PremiereReminderActivity.this.setListAdapter(saImageItems);

			if (PremiereReminderActivity.this.pd != null) {
				PremiereReminderActivity.this.pd.dismiss();
			}
		}
	}
}